/**************************************************************************************************
* Title: SumRenderWindow.cpp
* Author: Gael Huber
* Description: Defines a single Windows-based render window for drawing, etc.
**************************************************************************************************/
#include "SumRenderWindow.h"

/**************************************************************************************************
* The WindowsProcess function, handles basic Win32 message loop, etc.
**************************************************************************************************/
LRESULT CALLBACK WindowProcess(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message) {
	case WM_CLOSE:
		SetRunSimulation(false);
		return 0;
		
	// Initialize engine shutdown
	case WM_DESTROY:
		SetRunSimulation(false);
		return 0;
	}

	// Handle any messages which are not handled in the implemented message loop
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/**************************************************************************************************
* Constructor
**************************************************************************************************/
RenderWindow::RenderWindow(int w, int h, int nCmdShow) 
	: width(w), height(h)
{
	initialize(L"SumEngine", nCmdShow);
}

/**************************************************************************************************
* Constructor implementing width, height, and the class name for the window
**************************************************************************************************/
RenderWindow::RenderWindow(int w, int h, int nCmdShow, LPCWSTR windowName) 
	: width(w), height(h)
{
	initialize(windowName, nCmdShow);
}

/**************************************************************************************************
* Destructor
**************************************************************************************************/
RenderWindow::~RenderWindow(void) {
	// TODO
	// Release window, de-register class, etc.
	
	
	PostQuitMessage(0);
	// Do nothing
}

/**************************************************************************************************
* Initialize the window and window characteristics
**************************************************************************************************/
void RenderWindow::initialize(LPCWSTR windowName, int nCmdShow) {
	// Clear out the window class for use
	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	// Fill in the window struct with necessary information
	wc.cbSize = sizeof(WNDCLASSEX);		// Size of data in bytes
	wc.style = CS_HREDRAW | CS_VREDRAW;	// Horizontal and vertical redraw enabled
	wc.lpfnWndProc = WindowProcess;		// Function pointer to window process function
	wc.hInstance = GetHInstance();	// Set hInstance to be the application handle
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);	// Default arrow
	wc.lpszMenuName = NULL;
	wc.lpszClassName = windowName;

	// Register the window class
	if(!RegisterClassEx(&wc))
		return;

	// Set the window instance
	SetHInstance(wc.hInstance);

	// Adjust the window size
	RECT wr = {0, 0, width, height};
	AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
	
	// Calculate total window width and height
	int windowWidth = wr.right - wr.left;
	int windowHeight = wr.bottom - wr.top;

	SetHWND(CreateWindowEx(NULL,
		L"SumEngine",	// Name of the window class
		windowName,		// Title of the window
		WS_OVERLAPPEDWINDOW,	// Window style
		GetSystemMetrics(SM_CXSCREEN)/2 - windowWidth/2,	// x-position of the window
		GetSystemMetrics(SM_CYSCREEN)/2 - windowHeight/2,	// y-position of the window
		windowWidth,	// Window width
		windowHeight,	// Window height
		NULL,	// No parent window
		NULL,	// No menus
		wc.hInstance,	// Application handle
		NULL));	// Used with multiple windows

	// Display the window on the screen
	ShowCursor(FALSE);
	ShowWindow(GetHWND(), nCmdShow);
}